package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerCollection;
import com.woniu.mapper.FlowerCollectionMapper;
import com.woniu.service.FlowerCollectionService;
import com.woniu.vo.CollectionVoCZT;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FlowerCollectionServiceImpl implements FlowerCollectionService {
    @Resource
    private FlowerCollectionMapper fcm;

    @Override
    public FlowerCollection checkIsCollect(int accountId, int flowerId) {
        Example example = new Example(FlowerCollection.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("flowerId", flowerId);
        return fcm.selectOneByExample(example);
    }

    @Override
    public int insertCollection(int accountId, int flowerId) {
        FlowerCollection flowerCollection = new FlowerCollection();
        flowerCollection.setAccountId(accountId);
        flowerCollection.setFlowerId(flowerId);
        flowerCollection.setCreateTime(new Date());
        int insert = fcm.insertSelective(flowerCollection);
        return insert;
    }

    @Override
    public int deleteCollection(int accountId, int flowerId) {
        Example example = new Example(FlowerCollection.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("flowerId", flowerId);
        return fcm.deleteByExample(example);
    }

    @Override
    public PageInfo<CollectionVoCZT> selectAllCollectionByAccountId(int accountId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionVoCZT> list = fcm.selectAllCollectionByAccountId(accountId);
        return new PageInfo<>(list);
    }
}
